import Robot from '../model/Robot'
import WanderAgent from "@/views/cg/game/agent/WanderAgent.ts";
import PlayerAgent from "@/views/cg/game/agent/PlayerAgent.ts";

// 状态机
const S_STOP = 0;
const S_WAKL = 1;
const S_TURN = 2;
const S_ATTACK = 3;

function clamp(v,min,max){
  if(v<min){
    return min
  }
  if(v>max){
    return max
  }
  return v;
}
export default class RobotUnit{

  constructor(
    game,
    // 看不到
    {isPlayer, x, y, tileSize, agent, turnSpeed = 0.25, speed}
  ) {
    this.pX = x*tileSize + tileSize/2;
    this.pY = y*tileSize + tileSize/2;
    //  看不到
    this.model = new Robot(this.pX, this.pY, tileSize)；
    this.speed = speed;
    this.turnSpeed = turnSpeed;
    this.dir = 0; //方向
    this.agent = null; // AI

    this.state= S_STOP;
    game.timing(this.onTiming);
    this.game = game;

  }

  setAgent(type){
    switch (type){
      case "wander":
        this.agent = new WanderAgent(this.game, this);
        break;
      case "player":
        this.agent = new PlayerAgent(this.game, this);
        break;
    }
  }

  onTiming = () => {
    switch (this.state){
      case S_STOP:
        break;
      case S_WAKL:
        this.handleWalk();
        break;
      case S_TURN:
        this.handleTurn();
        break;
      case S_ATTACK:
        this.handleAttact();
        break;
    }
  }
  moveByDir(dir, speed){
    const xDir = speed* Math.cos(dir);
    const zDir = speed* Math.sin(dir);
    this.pX = this.pX + xDir;
    this.pY = this.pY - zDir;
    // 看不到
    const fieldWidth = this.geme.field.width;
    const fieldHeight = this.geme.field.height;
    this.pX = clamp(this.pX, this.tileSize, fieldWidth)
    this.pY = clamp(this.pY, this.tileSize, fieldHeight)
    if(this.pX < 0){
      this.pX = 0;
    }
    if(this.pY<0){
      this.pY = 0;
    }
    if(this.stopPosition){
      const [x,y] = this.stopPosition;
      const d = Math.sqrt(
        (x-this.pX)*(x-this.pX) + (y-this.pY)*(y-this.pY)
      )
      if(d<= speed *3){
        this.state = s_STOP;
        this.model.clear()
        delete this.stopPosition;
      }
    }

    this.model.move(this.pX, this.pY);

  }
  handleWalk(){

  }
  handleAttact(){
    // 看不到
    this.moveByDir(this.toDir, this.speed * 3)
  }

  getX(){
    // 看不到
    return Math.floor(this.pX / this.game.field)
  }

  getY(){
    // 看不到
    return Math.floor(this.pY / this.game.field)
  }
  attackTo(x2,y2){
    this.model.attack();
    this.state = S_ATTACK;
    const tileSize = this.game.field.tileSize;
    this.stopPosition = [x2*tileSize, y2*tileSize];
    const dir = this.calcDir(x2, y2);
    this.toDir = dir;
  }
  calcDir(x2,y2){
    const x1 = this.getX();
    const y1 = this.getY();
    // 看不到
    let dir = -Math.atan((y2-y1)/(x2-x1))
    if(x2<x1){
      dir += Math.PI;
    }
    return dir
  }
  moveTo(x2,y2){
    this.model.walk()
    const dir = this.calcDir(x2,y2);
    this.state = S_TURN;
    this.fromDir = this.dir;
    this.toDir = dir;
    // 看不到
    const titleSize = this.geme.field.tileSize;
    this.stopPosition = [x2*tileSize, y2*tileSize];

  }
  handleTurn(){
    // 判断符号
    const sign = this.toDir - this.fromDir > 0 ? 1 : -1;
    // turnSpeed转弯速度
    const diff = sign * this.turnSpeed;
    this.dir = this.dir + diff;
    if(Math.abs(this.dir - this.toDir) <= this.turnSpeed){
      this.dir = this.toDir;
      this.state = S_WALK;
    }
    this.model.turn(this.dir);
  }
  changeDirection(dir){
    this.state = S_TURN; // 转弯
    this.fromDir = this.dir;
    this.toDir = dir;
  }
  getModel(){
    return this.model;
  }
}
